package com.example.listgame;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends AppCompatActivity {

    private DBOpenHelper dbOpenHelper;  //声明DBOpenHelper对象
    private EditText mUsername,mPassword;
    private Button mCreate,mBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbOpenHelper = new DBOpenHelper(RegisterActivity.this,"db_userinfo.db",null,1);  //实例化DBOpenHelper对象，用来创建数据库
        mUsername = findViewById(R.id.username);
        mPassword = findViewById(R.id.password);
        mBack = findViewById(R.id.back);
        mCreate = findViewById(R.id.create);
        mCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usnm = mUsername.getText().toString();  //获取填写的昵称
                String pwd = mPassword.getText().toString();   //获取填写的密码
                if (usnm.equals("") || pwd.equals("")) {     //如果填写的昵称或密码为空时
                    Toast.makeText(RegisterActivity.this, "昵称或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
                    Cursor cursor = db.query("UserInfo", null, "username=?", new String[]{usnm}, null, null, null);
                    if (cursor != null && cursor.getCount() >= 1) {
                        Toast.makeText(RegisterActivity.this, "昵称已存在", Toast.LENGTH_SHORT).show();
                        cursor.close();
                    } else {
                        insertData(dbOpenHelper.getReadableDatabase(), usnm, pwd);  //添加用户
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    //插入数据的方法
    private void insertData (SQLiteDatabase sqLiteDatabase,String usnm,String pwd) {
        ContentValues values = new ContentValues();
        values.put("username",usnm);  //保存昵称
        values.put("password",pwd);  //保存密码
        sqLiteDatabase.insert("UserInfo",null,values); //执行插入操作
    }
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper!=null){
            dbOpenHelper.close();  //关闭数据库连接
        }
    }
}
